#!/bin/env bash

keagle_dir=/data/keagle
keagle_tgz=${keagle_dir}/kafka-eagle-bin-2.0.4.tar.gz
keagle_original_dir=${keagle_dir}/kafka-eagle-bin-2.0.4
keagle_home=${keagle_dir}/keagle
keagle_web_tgz=${keagle_home}/kafka-eagle-web-2.0.4-bin.tar.gz
keagle_web_original_dir=${keagle_home}/kafka-eagle-web-2.0.4-bin
keagle_web_home=${keagle_home}/keagle-web
keagle_web_config_dir=${keagle_web_home}/conf
zk_cluster="192.168.1.50:2181,192.168.1.51:2181,192.168.1.99:2181"
mysql="192.168.1.51"
mysql_usr=root
mysql_pwd=root
profile=/etc/profile

mkdir(){
    if [ ! -d "${keagle_dir}" ]; then
        echo "mk keagle dir"
        mkdir -p ${keagle_dir}
    fi
}

tar_keagle(){
    if [ -f "${keagle_tgz}" ] && [ ! -d ${keagle_home} ] ; then
        echo "tar keagle_tgz"
        tar xf ${keagle_tgz}
    fi
}

alias_keagle(){
    if [ ! -d ${keagle_home} ]; then
        echo "alias keagle"
        mv ${keagle_original_dir} ${keagle_home}
    fi
}


tar_keagle_web(){
    if [ -f "${keagle_web_tgz}" ] && [ ! -d ${keagle_web_home} ] ; then
        echo "tar keagle_web_tgz"
        tar xf ${keagle_web_tgz}
    fi
}

alias_keagle_web(){
    if [ ! -d ${keagle_web_home} ]; then
        echo "alias keagle web"
        mv ${keagle_web_original_dir} ${keagle_web_home}
    fi
}


cp_conf_file(){
    if [ ! -f "${keagle_web_config_dir}/system-config.properties.bak" ]; then
        echo "copy system-config.properties"
        cp ${keagle_web_config_dir}/system-config.properties ${keagle_web_config_dir}/system-config.properties.bak
    fi
}

write_keweb_system_config(){
    echo "write_keweb_system_config"
    cat << EOF > ${keagle_web_config_dir}/system-config.properties
kafka.eagle.zk.cluster.alias=cluster1
cluster1.zk.list=${zk_cluster}
######################################
# zookeeper enable acl
######################################
cluster1.zk.acl.enable=false
cluster1.zk.acl.schema=digest
cluster1.zk.acl.username=test
cluster1.zk.acl.password=test123

######################################
# broker size online list
######################################
cluster1.kafka.eagle.broker.size=20

######################################
# zk client thread limit
######################################
kafka.zk.limit.size=32

######################################
# kafka eagle webui port
######################################
kafka.eagle.webui.port=8048

######################################
# kafka jmx acl and ssl authenticate
######################################
cluster1.kafka.eagle.jmx.acl=false
cluster1.kafka.eagle.jmx.user=keadmin
cluster1.kafka.eagle.jmx.password=keadmin123
cluster1.kafka.eagle.jmx.ssl=false
cluster1.kafka.eagle.jmx.truststore.location=/Users/dengjie/workspace/ssl/certificates/kafka.truststore
cluster1.kafka.eagle.jmx.truststore.password=ke123456

######################################
# kafka offset storage
######################################
cluster1.kafka.eagle.offset.storage=kafka
cluster2.kafka.eagle.offset.storage=zk

######################################
# kafka jmx uri
######################################
cluster1.kafka.eagle.jmx.uri=service:jmx:rmi:///jndi/rmi://%s/jmxrmi

######################################
# kafka metrics, 15 days by default
######################################
kafka.eagle.metrics.charts=true
kafka.eagle.metrics.retain=15

######################################
# kafka sql topic records max
######################################
kafka.eagle.sql.topic.records.max=5000

######################################
# delete kafka topic token
######################################
kafka.eagle.topic.token=keadmin

######################################
# kafka sasl authenticate
######################################
cluster1.kafka.eagle.sasl.enable=false
cluster1.kafka.eagle.sasl.protocol=SASL_PLAINTEXT
cluster1.kafka.eagle.sasl.mechanism=SCRAM-SHA-256
cluster1.kafka.eagle.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="kafka" password="kafka-eagle";
cluster1.kafka.eagle.sasl.client.id=
cluster1.kafka.eagle.blacklist.topics=
cluster1.kafka.eagle.sasl.cgroup.enable=false
cluster1.kafka.eagle.sasl.cgroup.topics=
cluster2.kafka.eagle.sasl.enable=false
cluster2.kafka.eagle.sasl.protocol=SASL_PLAINTEXT
cluster2.kafka.eagle.sasl.mechanism=PLAIN
cluster2.kafka.eagle.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka" password="kafka-eagle";
cluster2.kafka.eagle.sasl.client.id=
cluster2.kafka.eagle.blacklist.topics=
cluster2.kafka.eagle.sasl.cgroup.enable=false
cluster2.kafka.eagle.sasl.cgroup.topics=

######################################
# kafka ssl authenticate
######################################
cluster3.kafka.eagle.ssl.enable=false
cluster3.kafka.eagle.ssl.protocol=SSL
cluster3.kafka.eagle.ssl.truststore.location=
cluster3.kafka.eagle.ssl.truststore.password=
cluster3.kafka.eagle.ssl.keystore.location=
cluster3.kafka.eagle.ssl.keystore.password=
cluster3.kafka.eagle.ssl.key.password=
cluster3.kafka.eagle.ssl.endpoint.identification.algorithm=https
cluster3.kafka.eagle.blacklist.topics=
cluster3.kafka.eagle.ssl.cgroup.enable=false
cluster3.kafka.eagle.ssl.cgroup.topics=

######################################
# kafka mysql jdbc driver address
######################################
kafka.eagle.driver=com.mysql.cj.jdbc.Driver
kafka.eagle.url=jdbc:mysql://${mysql}:3306/ke?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
kafka.eagle.username=${mysql_usr}
kafka.eagle.password=${mysql_pwd}
EOF
}

write_profile(){
    echo "write ke_home to ${profile}"
    {
        echo "#KAFKA EAGLE"
        echo "export KE_HOME=${keagle_web_home}"
        echo "export PATH=\$PATH:\$KE_HOME/bin"
    } >> ${profile}
}

insert_jhome_to_kesh(){
    if [ "" == "$(cat < ke.sh | grep "export JAVA_HOME")" ]; then
        echo "insert_jhome_to_kesh"
        sed -i "/^export/ i\export JAVA_HOME=\${JAVA_HOME}" ${keagle_web_config_dir}/bin/ke.sh
    fi
}

start_keagle_web(){
    if [ "" != "$(jps | grep KafkaEagle | awk -F " " '{print $2}')" ]; then
        echo "start KafkaEagle"
        ${keagle_web_home}/bin start
    fi
}

setup_keagle(){
    mkdir
    tar_keagle
    alias_keagle
    tar_keagle_web
    alias_keagle_web
    cp_conf_file
    write_keweb_system_config
    write_profile
    insert_jhome_to_kesh
    start_keagle_web
}


usage() {
    echo "useage:"
    echo "-h help"
    echo "-k setup"
    exit 0
}

while getopts 'hk' OPT;do
    case $OPT in
        k) setup_keagle "$OPTARG";;
        h) usage;;
        ?) usage;;
    esac
done

# 默认账号 admin
# 默认密码 123456
# 启动方式 bin/ke.sh start